package com.aaa.statistics.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aaa.entity.OrderBackfee;
import com.aaa.entity.OrderCharge;
import com.aaa.vo.Result;
import com.aaa.statistics.dao.OrderBackfeeMapper;
import com.aaa.statistics.dao.OrderChargeMapper;
import com.aaa.statistics.service.IOrderChargeService;
import com.aaa.statistics.vo.HisCareOrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 收费表 服务实现类
 * </p>
 *
 * @author chengchenchen
 * @since 2023-04-01
 */
@Service
public class OrderChargeServiceImpl extends ServiceImpl<OrderChargeMapper, OrderCharge> implements IOrderChargeService {
    @Autowired
    private OrderChargeMapper orderChargeMapper;
    @Autowired
    private OrderBackfeeMapper orderBackfeeMapper;

    /**
     * 获取统计表中所有数据
     * @param
     * @return
     */
    @Override
    public Result<Map<String,Object>> getInfo(HisCareOrderItemVo hisCareOrderItemVo) {
//        Double aggregateIncome = 0.0;//合计收入
//
//        Double generalIncome = 0.0;//总收入
//        Double cashPayment = 0.0;//现金支付
//        Double payAlipay = 0.0;//支付宝支付
//
//        Integer numPayment = 0;//总收入笔数
//        Integer numCashPayment = 0;//现金支付笔数
//        Integer numPayAlipay = 0;//支付宝支付笔数
//
//        Double totalRefund = 0.0;//总退费
//        Double cashRefund = 0.0;//现金退费
//        Double alipayRefund = 0.0;//支付宝退费

        BigDecimal aggregateIncome = new BigDecimal(0.0);//合计收入

        BigDecimal generalIncome = new BigDecimal(0.0);//总收入
        BigDecimal cashPayment = new BigDecimal(0.0);//现金支付
        BigDecimal payAlipay = new BigDecimal(0.0);//支付宝支付

        Integer numPayment = 0;//总收入笔数
        Integer numCashPayment = 0;//现金支付笔数
        Integer numPayAlipay = 0;//支付宝支付笔数

        BigDecimal totalRefund = new BigDecimal(0.0);//总退费
        BigDecimal cashRefund = new BigDecimal(0.0);//现金退费
        BigDecimal alipayRefund = new BigDecimal(0.0);//支付宝退费

        /**
         * 收费
         */
        QueryWrapper<OrderCharge> chargeQueryWrapper = new QueryWrapper<>();
        chargeQueryWrapper.eq("order_status","1");//支付成功的
        if (Objects.nonNull(hisCareOrderItemVo.getDateRange())){
            chargeQueryWrapper.between("pay_time",hisCareOrderItemVo.getDateRange()[0],hisCareOrderItemVo.getDateRange()[1]);
        }else {
            LocalDateTime now = LocalDateTime.now();
            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            chargeQueryWrapper.like("pay_time",format);//默认当前时间
        }//时间
        List<OrderCharge> orderCharges = orderChargeMapper.selectList(chargeQueryWrapper);//用于求收入
        for (OrderCharge orderCharge : orderCharges) {
//            generalIncome+=orderCharge.getOrderAmount().doubleValue();//计算出总收入
            generalIncome = generalIncome.add(orderCharge.getOrderAmount());//计算出总收入
        }
        numPayment = orderCharges.size();//总收入笔数

        chargeQueryWrapper.eq("pay_type","0");//设置现金支付
        List<OrderCharge> orderCharges1 = orderChargeMapper.selectList(chargeQueryWrapper);
        for (OrderCharge orderCharge : orderCharges1) {
//            cashPayment+=orderCharge.getOrderAmount().doubleValue();//计算出现金支付
            cashPayment = cashPayment.add(orderCharge.getOrderAmount());//计算出现金支付
        }
        numCashPayment = orderCharges1.size();//现金支付笔数
//        payAlipay=generalIncome-cashPayment;//计算出支付宝支付（总收入-现金支付）
        payAlipay=generalIncome.subtract(cashPayment);//计算出支付宝支付（总收入-现金支付）
        numPayAlipay = numPayment-numCashPayment;//计算出支付宝支付笔数

        /**
         * 退费
         */
        QueryWrapper<OrderBackfee> orderBackfeeWrapper = new QueryWrapper<>();
        orderBackfeeWrapper.eq("back_status","1");//退费成功的
        if (Objects.nonNull(hisCareOrderItemVo.getDateRange())){
            orderBackfeeWrapper.between("back_time",hisCareOrderItemVo.getDateRange()[0],hisCareOrderItemVo.getDateRange()[1]);
        }else {
            LocalDateTime now = LocalDateTime.now();
            String format = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            orderBackfeeWrapper.like("back_time",format);//默认当前时间
        }//时间
        List<OrderBackfee> orderBackfees = orderBackfeeMapper.selectList(orderBackfeeWrapper);
        for (OrderBackfee orderBackfee : orderBackfees) {
//            totalRefund+=orderBackfee.getBackAmount().doubleValue();//计算出总退费
            totalRefund = totalRefund.add(orderBackfee.getBackAmount());//计算出总退费
        }
        orderBackfeeWrapper.eq("back_type","0");//设置现金退费
        List<OrderBackfee> orderBackfees1 = orderBackfeeMapper.selectList(orderBackfeeWrapper);
        for (OrderBackfee orderBackfee : orderBackfees1) {
//            cashRefund+=orderBackfee.getBackAmount().doubleValue();//设置现金退款
            cashRefund = cashRefund.add(orderBackfee.getBackAmount());//设置现金退款
        }
//        alipayRefund=totalRefund-cashRefund;//计算出支付宝退款（总退款-现金退款）
        alipayRefund=totalRefund.subtract(cashRefund);//计算出支付宝退款（总退款-现金退款）

//        aggregateIncome=generalIncome-totalRefund;//计算出合计收入（收入-退费）
        aggregateIncome=generalIncome.subtract(totalRefund);//计算出合计收入（收入-退费）

        //存入Map中
        Map<String, Object> map = new HashMap<>();
        map.put("aggregateIncome",aggregateIncome);
        map.put("generalIncome",generalIncome);
        map.put("cashPayment",cashPayment);
        map.put("payAlipay",payAlipay);
        map.put("numCashPayment",numCashPayment);
        map.put("numPayAlipay",numPayAlipay);
        map.put("totalRefund",totalRefund);
        map.put("cashRefund",cashRefund);
        map.put("alipayRefund",alipayRefund);

        return new Result<>(2000,"查询总计成功",map);
    }
}
